package com.yupi.yupao.common;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Set;
import java.util.concurrent.TimeUnit;

@Component
public class RedisMatchHistoryService {

    private static final String MATCH_HISTORY_KEY_PREFIX = "user:matchHistory:";

    @Resource
    private RedisTemplate<String, Long> redisTemplate;

    // 获取用户的匹配历史集合
    public Set<Long> getMatchedUsers(Long userId) {
        return  redisTemplate.opsForSet().members(MATCH_HISTORY_KEY_PREFIX + userId);
    }

    // 将用户匹配过的用户添加到 Redis 中
    public void addMatchedUser(Long userId, Long matchedUserId) {
        redisTemplate.opsForSet().add(MATCH_HISTORY_KEY_PREFIX + userId, matchedUserId);
        // 可选：设置匹配历史记录的过期时间，例如7天过期
        redisTemplate.expire(MATCH_HISTORY_KEY_PREFIX + userId, 7, TimeUnit.DAYS);
    }

    // 清空用户的匹配历史
    public void clearMatchedUsers(Long userId) {
        redisTemplate.delete(MATCH_HISTORY_KEY_PREFIX + userId);
    }

    // 获取用户已匹配的用户数量
    public Long getMatchedUserCount(Long userId) {
        return redisTemplate.opsForSet().size(MATCH_HISTORY_KEY_PREFIX + userId);
    }
}

